package com.pureut.order.controller;

import com.pureut.common.core.domain.R;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.order.dto.PurchaseOrderDto;
import com.pureut.order.service.PurchaseOrderService;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.PurchaseOrderVo;
import com.pureut.system.api.vo.UpdatePurchaseOrderDataVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采购订单控制层
 */
@RestController
@RequestMapping("/purchase-order")
public class PurchaseOrderController extends BaseController {

    @Resource
    PurchaseOrderService purchaseOrderService;


    /**
     * 订单列表
     */
    @GetMapping("/list")
    @RequiresPermissions("order:purchase:subcontracting:list")
    public TableDataInfo list(PurchaseOrderVo purchaseOrderVo) {
        startPage();
        //查询供应商列表
        List<PurchaseOrderDto> list = purchaseOrderService.getList(purchaseOrderVo);
        return getDataTable(list);
    }

    /**
     * 计划确认
     */
    @RequiresPermissions("order:purchase:order:planConfirmation")
    @Log(title = "计划确认", businessType = BusinessType.INSERT)
    @PostMapping("/planConfirmation")
    public AjaxResult planConfirmation(@RequestBody PurchaseOrderVo purchaseOrderVo) throws Exception {
        return toAjax(purchaseOrderService.planConfirmation(purchaseOrderVo));
    }

    /**
     * 采购下单
     */
    @RequiresPermissions("order:purchase:order:placeOrder")
    @Log(title = "采购下单", businessType = BusinessType.UPDATE)
    @PostMapping("/placeOrder")
    public AjaxResult placeOrder(@Validated @RequestBody PurchaseOrderVo purchaseOrderVo) {
        return toAjax(purchaseOrderService.updateOrder(purchaseOrderVo));
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("order:purchase:order:subExamine")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReview/{id}")
    public AjaxResult submitReview(@PathVariable("id") Long id) {
        return toAjax(purchaseOrderService.submitReview(id));
    }

    /**
     * 撤销审核
     */
    @RequiresPermissions("order:purchase:order:revoke")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteReview/{id}")
    public AjaxResult deleteReview(@PathVariable("id") Long id) {
        return toAjax(purchaseOrderService.deleteReview(id));
    }

    /**
     * 关结
     */
    @RequiresPermissions("order:purchase:order:closing")
    @Log(title = "关结", businessType = BusinessType.UPDATE)
    @PostMapping("/closeReview")
    public AjaxResult closeReview(@RequestBody PurchaseOrderVo purchaseOrderVo) {
        return toAjax(purchaseOrderService.closeReview(purchaseOrderVo));
    }

    /**
     * 修改
     */
    @RequiresPermissions("order:purchase:order:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOrder")
    public AjaxResult updateOrder(@RequestBody PurchaseOrderVo purchaseOrderVo) {
        return toAjax(purchaseOrderService.updateOrderById(purchaseOrderVo));
    }

    /**
     * 查看订单信息
     */
    @RequiresPermissions("order:purchase:order:query")
    @GetMapping("/view/{id}")
    public R<?> view(@PathVariable("id") Long id) {
        return R.ok(purchaseOrderService.getViewById(id));
    }

    /**
     * 批次到货
     */
    @RequiresPermissions("order:purchase:order:planRelease")
    @Log(title = "批次到货", businessType = BusinessType.INSERT)
    @PostMapping("/batchArrival")
    public AjaxResult batchArrival(@RequestBody com.pureut.system.api.dto.PurchaseOrderDto purchaseOrderDto) {
        return toAjax(purchaseOrderService.batchArrival(purchaseOrderDto));
    }

    /**
     * 审核单据
     */
    @RequiresPermissions("order:purchase:order:review")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PostMapping("/purchaseOrderReview")
    public AjaxResult purchaseOrderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(purchaseOrderService.purchaseOrderReview(orderReviewVo));
    }

    /**
     * 修改采购订单数据（feign调用）
     */
    @PostMapping("/updateOrderData")
    public AjaxResult updateOrderData(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return AjaxResult.success(purchaseOrderService.updateOrderData(updatePurchaseOrderDataVo));
    }

    /**
     * 修改采购订单退货重量（累加）Feign
     */
    @PostMapping("/updateSalesReturn")
    public boolean updateSalesReturn(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return purchaseOrderService.updateSalesReturn(updatePurchaseOrderDataVo);
    }

    /**
     * 修改采购订单退货重量（扣除）Feign
     */
    @PostMapping("/updateDeductSalesReturn")
    public boolean updateDeductSalesReturn(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return purchaseOrderService.updateDeductSalesReturn(updatePurchaseOrderDataVo);
    }

}
